package charts.series.bars ;

import flash.filters.DropShadowFilter;
import flash.geom.Matrix;
import charts.series.bars.Base;

class Glass extends Base
{
	
	public function new ( index: Int, style:Dynamic, group: Float ) {
		
		
		super(index, style, style.color, style.tip, style.alpha, group);
		
		var dropShadow:DropShadowFilter = new flash.filters.DropShadowFilter();
		dropShadow.blurX = 5;
		dropShadow.blurY = 5;
		dropShadow.distance = 3;
		dropShadow.angle = 45;
		dropShadow.quality = 2;
		dropShadow.alpha = 0.4;
		// apply shadow filter
		this.filters = [dropShadow];
	}
	
	public override function resize ( sc:ScreenCoordsBase ):Void {
		
		this.graphics.clear();
		var h:Dynamic = this.resize_helper( cast(sc,ScreenCoords));
		if (h.height == 0)
			return;
		
		this.bg( h.width, h.height, h.upside_down );
		this.glass( h.width, h.height, h.upside_down );
	}
	
	function bg ( w: Float, h: Float, upside_down:Bool ):Void {
		//
		var rad: Float = 7;
		if ( rad > ( w / 2 ) )
			rad = w / 2;
			
		this.graphics.lineStyle(0, 0, 0);// this.outline_color, 100);
		this.graphics.beginFill(this.color, 1); // TODO - wtf is wrong? Float should be Int? Don't think so...
		
		if( !upside_down )
		{
			// bar goes up
			this.graphics.moveTo(0+rad, 0);
			this.graphics.lineTo(w-rad, 0);
			this.graphics.curveTo(w, 0, w, rad);
			this.graphics.lineTo(w, h);
			this.graphics.lineTo(0, h);
			this.graphics.lineTo(0, 0+rad);
			this.graphics.curveTo(0, 0, 0+rad, 0);
		}
		else
		{
			// bar goes down
			this.graphics.moveTo(0, 0);
			this.graphics.lineTo(w, 0);
			this.graphics.lineTo(w, h-rad);
			this.graphics.curveTo(w,h,w-rad, h);
			this.graphics.lineTo(rad, h);
			this.graphics.curveTo(0,h,0, h-rad);
			this.graphics.lineTo(0, 0);
		}
		this.graphics.endFill();
	}
	
	function glass ( w: Float, h: Float, upside_down:Bool ): Void {
		var x: Float = 2;
		var y: Float = x;
		var width: Float = (w/2)-x;
		
		if( upside_down )
			y -= x;
		
		h -= x;
		
		this.graphics.lineStyle(0, 0, 0);
		//set gradient fill
		var colors:Array<Float> = [0xFFFFFF,0xFFFFFF];
		var alphas:Array<Float> = [0.3, 0.7];
		var ratios:Array<Int> = [0,255];
		//var matrix:Dynamic = { matrixType:"box", x:x, y:y, w:width, h:height, r:(180/180)*Math.PI };
		//mc.beginGradientFill("linear", colors, alphas, ratios, matrix);
		var matrix:Matrix = new Matrix();
		matrix.createGradientBox(width, height, (180 / 180) * Math.PI );
		this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, alphas, ratios, matrix, 'pad'/*SpreadMethod.PAD*/ );
		
		var rad: Float = 4;
		var w: Float = width;
		
		if( !upside_down )
		{
			this.graphics.moveTo(x+rad, y);		// <-- top
			this.graphics.lineTo(x+w, y);
			this.graphics.lineTo(x+w, y+h);
			this.graphics.lineTo(x, y+h);
			this.graphics.lineTo(x, y+rad);
			this.graphics.curveTo(x, y, x+rad, y);
		}
		else
		{
			this.graphics.moveTo(x, y);
			this.graphics.lineTo(x+w, y);
			this.graphics.lineTo(x+w, y+h);
			this.graphics.lineTo(x + rad, y + h);
			this.graphics.curveTo(x, y+h, x, y+h-rad);
		}
		this.graphics.endFill();
	}
}
